use std::cmp::max;

struct Solution {}

fn main() {}

impl Solution {
    pub fn min_sum(nums1: Vec<i32>, nums2: Vec<i32>) -> i64 {
        let cnt_z1 = nums1.iter().filter(|&&e| e == 0).count() as i32;
        let cnt_z2 = nums2.iter().filter(|&&e| e == 0).count() as i32;
        let sum_1: i64 = Self::get_sum_i64(&nums1);
        let sum_2: i64 = Self::get_sum_i64(&nums2);
        if sum_1 > sum_2 {
            return Self::min_sum_impl(nums2, nums1, cnt_z2, cnt_z1);
        }
        Self::min_sum_impl(nums1, nums2, cnt_z1, cnt_z2)
    }

    fn min_sum_impl(nums_min: Vec<i32>, nums_max: Vec<i32>, cnt_z_min: i32, cnt_z_max: i32) -> i64 {
        let sum_max = Self::get_sum_i64(&nums_max);
        let new_sum_max: i64 = sum_max + cnt_z_max as i64;
        let sum_min: i64 = Self::get_sum_i64(&nums_min);
        let new_sum_min = sum_min + ((cnt_z_min * 1) as i64);
        if cnt_z_max == 0 && cnt_z_min == 0 {
            return if sum_max != sum_min { -1 } else { sum_max };
        }
        if cnt_z_min == 0 {
            return -1;
        }
        if cnt_z_max == 0 {
            return if new_sum_min > sum_max { -1 } else { sum_max };
        }
        max(new_sum_max, new_sum_min)
    }

    fn get_sum_i64(nums: &Vec<i32>) -> i64 {
        let mut ret: i64 = 0;
        for num in nums {
            ret += *num as i64;
        }
        ret
    }
}
